// pages/details/details.js
import user_login from '../../module/user_login';
import user_thing from '../../module/use_thing';
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    // 控制
    isOK: true,
    isSaled: false,
    none: false,
    ssfw: false,
    isreplyShow: false,
    isMe: false,
    reply: '',
    focus: false,
    re: false,
    // 数据
    category_num: 1,
    id: 0,
    // 评论
    commentArr: []
  },
  /**
   * 跳转他人页面
   */
  goThers(e) {
    // console.log(this.data.owner.user_id)
    wx.navigateTo({
      url: '../others/others?user_id=' + this.data.owner.user_id
    });
  },
  /**
   * 查看 图片
   */
  imgTap(e) {
    wx.previewImage({
      current: this.data.images[e.target.dataset.num],
      urls: this.data.images
    });
  },
  /**
   * 前往专题页面
   */
  goBanner(e) {
    console.log(e);
    wx.navigateTo({
      url: '../banner/banner?data=' + JSON.stringify(this.data.topic)
    });
  },
  /**
   * 分类跳转
   */
  sortClick(e) {
    // console.log(this.data)
    let that = this;
    wx.navigateTo({
      url: '../search/search?title=' + that.data.category_CN + '&category=' + that.data.category
    });
  },
  /**
   * 已出售
   */
  thingStatus() {
    let that = this;

    wx.showModal({
      title: '已出售？',
      content: '',
      confirmText: '确认',
      success(res) {
        if (res.confirm) {
          user_thing
            .setThingStatus(that.data.id)
            .then(res => {
              console.log(res);
              //
              wx.showToast({
                title: '物品已出售！',
                icon: 'success',
                duration: 1000
              });
              setTimeout(() => {
                wx.navigateBack({
                  delta: 1
                });
              }, 1000);
            })
            .catch(err => {
              console.error(err);

              wx.showToast({
                title: '失败',
                image: '../../image/dele.png',
                duration: 1000
              });
            });
        } else if (res.cancel) {
          // console.log('用户点击取消')
        }
      }
    });
  },
  /**
   * 分享
   */
  toOther() {
    console.log('分享');
  },
  /**
   * btn留言点击
   */
  replyShow(bo = true) {
    this.setData({
      isreplyShow: bo,
      focus: bo
    });
  },
  /**
   * 获取评论
   */
  getComment() {
    user_thing.comments(this.data.id).then(res => {
      // console.log(res)
      this.setData({
        commentArr: res
      });
    });
  },

  /**
   * 发送 留言
   */
  replyOk(e) {
    if (this.data.reply) {
      let that = this,
        formId = e.detail.formId;

      if (this.data.re) {
        user_thing.subComments(this.data.com.id, this.data.com.userid, this.data.reply, this.data.id, formId).then(res => {
          run();
        });
      } else {
        user_thing.postComments(this.data.id, this.data.reply, formId).then(res => {
          run();
        });
      }
      function run(obj = {}) {
        that.setData({
          reply: '',
          re: false,
          ...obj
        });
        that.replyShow(false);
        that.getComment();
      }
    } else {
      wx.showToast({
        title: '输入不能为空',
        image: '../../image/dele.png',
        duration: 750
      });
    }
  },
  /**
   * 评论输入 失去焦点
   */
  inputBlur() {
    //
    setTimeout(() => {
      this.replyShow(false);
    }, 250);
  },
  /**
   * 回复评论
   */
  subComment(e) {
    // console.log(e.currentTarget.dataset)
    this.replyShow();
    this.setData({
      re: true,
      com: {
        id: e.currentTarget.dataset.id,
        userid: e.currentTarget.dataset.userid
      }
    });
  },
  /**
   * 留言输入
   * @param {Obj} e
   */
  replyIn(e) {
    this.setData({
      reply: e.detail.value
    });
  },

  /**
   * 前往绑定教务系统
   */
  goLogin() {
    wx.navigateTo({
      url: '../login/login'
    });
  },
  /**
   * 我想要
   */
  iWand(e) {
    let that = this;
    wx.showActionSheet({
      itemList: ['打电话', 'QQ联系'],
      success(res) {
        console.log(res.tapIndex);
        switch (res.tapIndex) {
          case 0:
            // 电话
            wx.makePhoneCall({
              phoneNumber: that.data.contacts.tel
            });
            break;
          case 1:
            // qq
            wx.showModal({
              title: that.data.contacts.qq,
              content: ' ',
              confirmText: '复制QQ号',
              success(res) {
                if (res.confirm) {
                  wx.setClipboardData({
                    data: that.data.contacts.qq,
                    success(res) {
                      wx.showToast({
                        title: 'QQ号复制成功',
                        icon: 'success',
                        duration: 2000
                      });
                    },
                    fail() {
                      wx.showToast({
                        title: 'QQ号复制失败',
                        image: '../../image/dele.png',
                        duration: 1000
                      });
                    }
                  });
                } else if (res.cancel) {
                  //  console.log('用户点击取消')
                }
              }
            });
            break;
          default:
            break;
        }
      },
      fail(res) {
        console.log(res.errMsg);
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;
    user_thing
      .getOneThing(options.id)
      .then(res => {
        console.log(res.data);
        this.setData(
          {
            ...res.data
          },
          () => {
            // 获取评论
            this.getComment();

            // 判断是否本人
            user_login.myInfo().then(res2 => {
              if (res2.data.data.user_id == res.data.owner.user_id) {
                this.setData({
                  isMe: true,
                  isOK: false
                });
              } else {
                this.setData({
                  isMe: false,
                  isOK: false
                });
              }
            });
            // 判断是否出售
            if (res.data.status == 'saled') {
              this.setData({
                isSaled: true
              });
            }
          }
        );
      })
      .catch(err => {
        console.log('-1', err);
        this.setData({
          isOK: false,
          none: true
        });
      });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    user_login.checkSSF(
      () => {
        this.setData({
          ssfw: true
        });
      },
      () => {
        this.setData({
          ssfw: false
        });
      }
    );
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    let that = this;
    return {
      title: '快来看看我的东西吧！',
      path: 'pages/index/index?id=' + that.data.id,
      success: function(res) {
        // 转发成功
      },
      fail: function(err) {
        // 转发失败
      }
    };
  },
  backHome: function() {
    wx.reLaunch({
      url: '../index/index'
    });
  }
});
